package com.example.delll.kugou.fragment;


import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.delll.kugou.R;
import com.example.delll.kugou.adapter.RecyclerViewAdapter;
import com.example.delll.kugou.pojo.ListenUltra;
import com.example.delll.kugou.utils.OkHttp;
import com.google.gson.Gson;
import java.io.IOException;

/**
 * A simple {@link Fragment} subclass.
 */
public class TouTiaoFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener {
    private static final String TAG = "MainActivity";
    private RecyclerView rv_show;
    private RecyclerViewAdapter recyclerViewAdapter;
    private SwipeRefreshLayout srl;
    private boolean isSlidingUpward = false;

    public TouTiaoFragment() {
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_tou_tiao, container, false);
    }


    @Override
    public void onResume() {
        super.onResume();
        Bundle arguments = getArguments();
        int index = arguments.getInt("index");
        if (index==1) {
            loadData();
        }
    }

    /**
     * 初始化视图  设置监听事件
     * @param savedInstanceState
     */
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        rv_show = getView().findViewById(R.id.rv_show);
        srl = getView().findViewById(R.id.srl);
        srl.setOnRefreshListener(this);
        rv_show.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false));
        recyclerViewAdapter = new RecyclerViewAdapter(getContext());
        rv_show.setAdapter(recyclerViewAdapter);
        rv_show.setOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                LinearLayoutManager manager = (LinearLayoutManager) recyclerView.getLayoutManager();
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    int lastItemPosition = manager.findLastCompletelyVisibleItemPosition();
                    int itemCount = manager.getItemCount();
                    if (lastItemPosition == itemCount - 1 & isSlidingUpward) {
                        loadData();
                    }
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                isSlidingUpward=dy>0;
            }
        });
    }


    /**
         *请求数据
         */
    private void loadData() {
        mHandler.sendEmptyMessageDelayed(1, 0);
        OkHttp.getJson(OkHttp.a1, new OkHttp.OkHttpUtils() {
            @Override
            public void onFailure(IOException e) {

            }

            @Override
            public void onResponse(String json) {
                ListenUltra listenUltra = new Gson().fromJson(json, ListenUltra.class);
                recyclerViewAdapter.setListenUltra(listenUltra);
//                mHandler.sendEmptyMessageDelayed(2, 2000);
                mHandler.sendEmptyMessageDelayed(3, 2000);
            }
        });
    }

    boolean is;

    @Override
    public void onRefresh() {
        mHandler.sendEmptyMessageDelayed(0, 2000);
    }

    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case 0:
                    srl.setRefreshing(false);
                    break;
                case 1:
                    recyclerViewAdapter.setLoadState(recyclerViewAdapter.LOADING);
                    break;
                case 2:
                    recyclerViewAdapter.setLoadState(recyclerViewAdapter.LOADING_COMPLETE);
                    break;
                case 3:
                    recyclerViewAdapter.setLoadState(recyclerViewAdapter.LOADING_END);
                    break;
            }
        }
    };

}
